<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mltv extends MY_Controller {

    public function index() {
        $this->load->view('mltv_wall');
    }
	public function login($username, $password){
		$this->load->model('users_model');
		$user = new Users_model();
		$result = $user->getAll(array('email_eq' => $username, 'password' => md5($password)));
		if($result->num_rows() > 0){
			$userdata = array();
			$userdata = $result->row();
			$userdata['userdata'] = $userdata;
			$this->session->set_userdata('logged_id', true);
			$this->session->set_userdata($userdata);
			print json_encode(array('result' => 1, 'message' => 'Success!'));
			return true;
		}else{
			print json_encode(array('result' => 0, 'message' => 'Error! The user credentials are incorrect!'));
			return false;
		}
	}
	
	
	public function logout(){
		$this->session->unset_userdata('logged_in');
		$this->session->sess_destroy();
		print json_encode(array('result' => 1, 'message' => 'Success!'));
	}
	public function authorized(){
		$logged_id = $this->session->userdata('logged_in');
		if(!empty($logged_id) && $logged_id == true){
			return true;
		}else{
			return false;
		}
	}
	public function acl($role_id, $user_id){
		$this->load->model('user_roles_model');
		$result = $this->user_roles_model->getAll(array('role_id_eq' => $role_id, 'user_id_eq' => $user_id));
		if($result->num_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	public function sendPrivateMessage(){
		if(
	}
    public function getMessagesByPages() {
        $page = $this->input->post('page');
        $limit = $this->input->post('rows');
        $this->load->model('messages_model');
        $filter['not_share'] = "not";
        $messages = $this->messages_model->getByParams($limit, $dir = 'DESC', $sort = 'dt', $page, $filter);
        $total = $this->messages_model->getAll($filter)->num_rows();
        $data = array();
        $result = array();
        $result['total'] = $total;
        $result['page'] = $page;
        if ($messages) {
            $data['messages'] = $messages->result();
            $data['total'] = $total;
            $data['page'] = $page;
            $result['result'] = 1;
            $result['message'] = $this->load->view('messagesByPages', $data, true);
        } else {
            $result['result'] = 0;
            $result['message'] = $this->load->view('error');
        }
        print json_encode($result);
    }

    public function sendMessage() {
        $form = $_POST;
        $this->load->model('messages_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('email', 'E-mail', 'required');
        $this->form_validation->set_rules('phone', 'Phone', 'required');
        $this->form_validation->set_rules('share', 'Share', 'required');
        $this->form_validation->set_rules('message', 'Message', 'required');

        $result = array();
        if ($this->form_validation->run() == FALSE) {
            $errors = validation_errors();
            if (strlen(trim($errors))) {
                $result = array('result' => '0', 'message' => $errors);
                print json_encode($result);
                return;
            }
        } else {
            $msg = new Messages_model();
			$form['dt'] = date('Y-m-d h:i:s');
			$form['ip'] = $_SERVER['REMOTE_ADDR'];
            $msg->setData($form);
            $msg->save();
        }
        $result = array('result' => '1', 'message' => 'Success!');
        print json_encode($result);
    }

    public function getSendForm() {
        $result = array();
        $result['result'] = 1;
        $result['message'] = $this->load->view('sendForm', array(), true);
        print json_encode($result);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */